package com.sunnada.edu.system.pub.dao;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.util.Utils;


/***
 * explain 分页信息
 * 
 * @author chs 2014-10-20 version 1.0
 */
public class Page {
	private int currentPage;
	private int rowCount;
	private int pageSize;

	public int getCurrentPage() {
		if (currentPage < 1) {
			return 1;
		}
		int pageCount = getPageCount();
		if (pageCount > 0 && currentPage > pageCount) {
			return 0;
		}
		return currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getPageCount() {
		if ((rowCount != 0) && (pageSize != 0)) {
			return ((rowCount + pageSize) - 1) / pageSize;
		}
		return 0;
	}

	public void setPageMap(HttpServletRequest request, Page page,
			Map<String, String> params) {
		int cp = 1;
		String curr = request.getParameter("curr");
		if (Utils.isNotNull(curr)) { 
			cp = Integer.valueOf(curr);
		} 
		page.setCurrentPage(cp);
		String height=(String)request.getSession().getAttribute(Constants.USER_SCREEN_HEIGHT);
		//根据commonData.js  中 setCommonHH（）配置
		String th=(String)request.getParameter("hh");
		//String th="250";
		if(th!=null){
			height=th;
		}
		int h = 800; 
		if(height!=null)h=Integer.valueOf(height);		

		
		if(params.get(Constants.PAGE_SIZE)!=null){ 
			page.setPageSize(Integer.valueOf(params.get(Constants.PAGE_SIZE)));
		}else{
			int devHeight = 0;
			if(params.get(Constants.DEV_PAGE_HEIGHT)!=null){ 
				devHeight=Integer.valueOf(params.get(Constants.DEV_PAGE_HEIGHT));
			}
			//page.setPageSize((h-devHeight) / 36);
			page.setPageSize(10);
		}
		
		int beginIndex = page.getCurrentPage() * page.getPageSize();
		int s = beginIndex - page.getPageSize();
		int e = beginIndex + 1;
		params.put("sr", s + "");
		params.put("ps", page.getPageSize() + "");
		params.put("er", e + "");
		params.put("curr", page.getCurrentPage() + "");
		params.put("rc", page.getRowCount() + "");
		params.put("pc", page.getPageCount() + "");
		int cr = page.getCurrentPage() < (page.getRowCount()
				/ page.getPageSize() + 1) ? page.getPageSize() : page
				.getRowCount() % page.getPageSize();
				params.put("cr", cr + "");
	}
	
	public void setThumbnailsPageMap(HttpServletRequest request, Page page,
			Map<String, String> params) {
		int cp = 1;
		String curr = request.getParameter("curr");
		if (Utils.isNotNull(curr)) { 
			cp = Integer.valueOf(curr);
		} 
		page.setCurrentPage(cp);
		String height=(String)request.getSession().getAttribute(Constants.USER_SCREEN_HEIGHT);
		String th=(String)request.getParameter("hh");
		String ww = Utils.toString(request.getParameter("ww"));
		if(th!=null){
			height=th;
		}
		int h = 600; 
		if(height!=null)h=Integer.valueOf(height);		

		int w = 800; 
		if(ww!=null)w=Integer.valueOf(ww);	
		
		if(params.get(Constants.PAGE_SIZE)!=null){ 
			page.setPageSize(Integer.valueOf(params.get(Constants.PAGE_SIZE)));
		}
		int devHeight = 0;
		if(params.get(Constants.DEV_PAGE_HEIGHT)!=null){ 
			devHeight=Integer.valueOf(params.get(Constants.DEV_PAGE_HEIGHT));
		}
		page.setPageSize(Integer.parseInt(Utils.toString(params.get("pageSize"))));
		int beginIndex = page.getCurrentPage() * page.getPageSize();
		int s = beginIndex - page.getPageSize();
		int e = beginIndex + 1;
		params.put("sr", s + "");
		params.put("ps", page.getPageSize() + "");
		params.put("er", e + "");
		params.put("curr", page.getCurrentPage() + "");
		params.put("rc", page.getRowCount() + "");
		params.put("pc", page.getPageCount() + "");
		int cr = page.getCurrentPage() < (page.getRowCount()
				/ page.getPageSize() + 1) ? page.getPageSize() : page
				.getRowCount() % page.getPageSize();
				params.put("cr", cr + "");
	}

}
